package com.slz3.business.module.memberManagement.memMember.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.redis.Jedis;
import com.slz3.business.frame.redis.RK;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.memberManagement.memMember.domain.MemMember;
import com.slz3.business.module.memberManagement.memMember.domain.MemMemberFactory;
import com.slz3.business.module.memberManagement.memMember.steward.MemMemberSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Slf4j
@Service
public class MemMemberServiceImpl implements MemMemberService {

    @Override
    public MemMember save(MemMember memMember) {
        MemMember memMemberDB = Repositorys.memMember.save(MemMemberFactory.initBaseFields(memMember));
//        if(VE.isNotEmpty(Jedis.db0.getMap(RK.USER_INFO.concat(":").concat(memMemberDB.getUuid().toString())))){
//            Jedis.db0.remove(RK.USER_INFO.concat(":").concat(memMemberDB.getUuid().toString()));
//        }
//        Jedis.db0.set(RK.USER_INFO.concat(":").concat(memMemberDB.getUuid().toString()),Steward.obj2Map(memMemberDB));
//        Jedis.db0.set(RK.USER_PHONEMEMMEMBER.concat(":").concat(memMemberDB.getSerPhone()),memMemberDB.getUuid().toString());
        return memMemberDB;
    }

    @Override
    public Page<MemMember> page(MultiValueMap<String, String> params) {
        Page<MemMember> memMemberPage = Repositorys.memMember.findAll(
                MemMemberSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return memMemberPage;
    }

    @Override
    public MemMember modify(MemMember memMemberNew) {
        MemMember memMemberDB = Jedis.db0.get(RK.USER_UPDATEUSER.concat(":").concat(memMemberNew.getUuid().toString()));
//        if(VE.isNotEmpty(memMemberDB)){
//            Jedis.db0.remove(RK.USER_UPDATEUSER.concat(":").concat(memMemberDB.getUuid().toString()));
//            memMemberDB = Repositorys.memMember.saveAndFlush(MemMemberFactory.initBaseFields(memMemberNew));
//        }
//        Jedis.db0.set(RK.USER_UPDATEUSER.concat(":").concat(memMemberDB.getUuid().toString()),memMemberDB);
        return memMemberDB;
    }



    @Override
    public Optional<MemMember> single(UUID uuid) {
        Optional<MemMember> memMember = Jedis.db0.get(RK.USER_UPDATEUSER.concat(":").concat(uuid.toString()));
        if(VE.isEmpty(memMember)){
            memMember = Repositorys.memMember.findById(uuid);
        }
        return memMember;
    }


    @Override
    public Optional<MemMember> findById(String userId) {
        return Repositorys.memMember.findById(UUID.fromString(userId));
    }

    @Override
    public List<MemMember> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.memMember.findAllById(uuids);
    }

    @Override
    public List<MemMember> delete(List<MemMember> memMemberList) {
        memMemberList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            MemMemberFactory.initBaseFields(item);
            Jedis.db0.remove(RK.USER_UPDATEUSER.concat(":").concat(item.getUuid().toString()));
        });
        return Services.memMemberTransactional.batchModify(memMemberList);
    }

}
